// yarn add redux react-readux
/*
  redux 是一种数据管理模型 跟react没什么关系
  react-redux  是把redux应用在了 react中
*/
import { createStore, combineReducers } from 'redux'
import * as types from './action_types'
function reducer(state, action) {
  console.log('count的 reducer执行了')
  state = state || {
    count: 100,
    name: "珠峰",
    qqq: 100
  }
  switch (action.type) {
    case types.COUNTADD:
      return {
        ...state,
        count: state.count + action.qqq
      }
    case types.COUNTMINUS:
      return {
        ...state,
        count: state.count - action.www
      }

    default:
      return {
        ...state
      }
  }
}
function colorReducer(state, action) {

  console.log('color的 reducer执行了')
  state = state || {
    color: "red"
  }
  switch (action.type) {
    case types.CHANGECOLOR:
      return {
        ...state,
        color: action.col
      }
    case types.COLORADD:
      return {
        color: '#ccc'
      }
    default:
      return {
        ...state
      }
  }
}
// combineReducers 合并多个reducer的
/* 
  redux 的dispatch执行的时候 会把所有的reducer全部执行了

*/
let rootReducer = combineReducers({
  countReducer: reducer,
  colorReducer: colorReducer
})

let store = createStore(rootReducer)
export default store